package com.rongma.waterdrop.manager;

import android.content.Context;

import com.rongma.waterdrop.listener.DataFromBmobListener;
import com.rongma.waterdrop.listener.getBmob.GetAllUserListener;
import com.rongma.waterdrop.listener.getBmob.GetDefaultNewsListener;
import com.rongma.waterdrop.listener.getBmob.GetEventListener;
import com.rongma.waterdrop.listener.getBmob.GetTaskListener;
import com.rongma.waterdrop.listener.getBmob.GetTimeListener;
import com.rongma.waterdrop.manager.bmobmanager.DefaultNewsManager;
import com.rongma.waterdrop.manager.bmobmanager.TaskManager;
import com.rongma.waterdrop.manager.bmobmanager.TimeManager;
import com.rongma.waterdrop.manager.bmobmanager.TimerEventManager;
import com.rongma.waterdrop.manager.bmobmanager.UserManager;
import com.rongma.waterdrop.model.DefaultNews;
import com.rongma.waterdrop.model.Task;
import com.rongma.waterdrop.model.Time;
import com.rongma.waterdrop.model.TimerEvent;
import com.rongma.waterdrop.model.User;
import com.rongma.waterdrop.network.api.GetAllUserAPI;
import com.rongma.waterdrop.network.api.GetDefaultNewsAPI;
import com.rongma.waterdrop.network.api.GetEventAPI;
import com.rongma.waterdrop.network.api.GetTaskAPI;
import com.rongma.waterdrop.network.api.GetTimeAPI;
import com.rongma.waterdrop.utils.LogUtils;

import java.util.ArrayList;

/**
 * Created by RongMa on 16/6/6.
 */

public class DownloadManager
{
    private static final DownloadManager manager = new DownloadManager();

    private DataFromBmobListener listener;

    private DownloadManager() {}

    public static DownloadManager getManager()
    {
        return manager;
    }

    public void setDownloadListener(DataFromBmobListener listener)
    {
        this.listener = listener;
    }

    //从网络端获取User数据
    public synchronized void downloadUserData(final Context context)
    {
        GetAllUserAPI allUserAPI = new GetAllUserAPI(new GetAllUserListener() {
            @Override
            public void onSuccess(ArrayList<User> list) {
                LogUtils.show("活捉User数据"+list.size()+"个");
                //将数据存入数据库
                if(list.size() > 0)
                {
                    UserManager.getManager(context).insertUserList(list, context);
                }
                listener.onUserSuccess();

            }

            @Override
            public void onError(String s) {
                LogUtils.show("从Bmob云后台获取User数据失败!");
                listener.onFail(0);
            }
        });
        //从网络获取数据
        allUserAPI.getDataFromBmob(context);
    }

    //从网络端获取DefaultNews数据
    public synchronized void downloadDefaultNewsData(final Context context)
    {
        GetDefaultNewsAPI defaultNewsAPI = new GetDefaultNewsAPI(new GetDefaultNewsListener() {
            @Override
            public void onSuccess(ArrayList<DefaultNews> list) {
                LogUtils.show("活捉DefaultNews数据"+list.size()+"个");
                //将数据存入数据库
                if(list.size() > 0) {
                    DefaultNewsManager.getManager(context).saveDefaultNewsList(list);
                }
                listener.onNewsSuccess();
            }

            @Override
            public void onError(String s) {
                LogUtils.show("从Bmob云后台获取DefaultNews数据失败!");
                listener.onFail(1);
            }
        });
        defaultNewsAPI.getDataFromBmob(context);
    }

    //从网络端获取Task数据
    public synchronized void downloadTaskData(final Context context)
    {
        GetTaskAPI taskAPI = new GetTaskAPI(new GetTaskListener() {
            @Override
            public void onSuccess(ArrayList<Task> list) {
                LogUtils.show("活捉Task数据"+list.size()+"个");
                if(list.size() > 0) {
                    TaskManager.getManager(context).addTaskListToDB(list);
                }
                listener.onTaskSuccess();
            }

            @Override
            public void onError(String s) {
                LogUtils.show("从Bmob云后台获取Task数据失败!");
                listener.onFail(2);
            }
        });
        taskAPI.getDataFromBmob(context);
    }

    //从网络端获取Time数据
    public synchronized void downloadTimeData(final Context context)
    {
        GetTimeAPI timeAPI = new GetTimeAPI(new GetTimeListener() {
            @Override
            public void onSuccess(ArrayList<Time> list) {
                LogUtils.show("活捉Time数据"+list.size()+"个");
                if(list.size() > 0) {
                    TimeManager.getManager(context).insertTimeList(list);
                }
                listener.onTimeSuccess();
            }

            @Override
            public void onError(String s) {
                LogUtils.show("从Bmob云后台获取Time数据失败!");
                listener.onFail(3);
            }
        });
        timeAPI.getDataFromBmob(context);
    }

    public synchronized void downloadTimeEventData(final Context context)
    {
        GetEventAPI eventAPI = new GetEventAPI(new GetEventListener() {
            @Override
            public void onSuccess(ArrayList<TimerEvent> list)
            {
                LogUtils.show("活捉Event数据"+list.size()+"个");
                if(list.size() > 0) {
                    TimerEventManager.getManager(context).insertTimerList(list);
                }
                listener.onEventSuccess();
            }

            @Override
            public void onError(String s)
            {
                LogUtils.show("从Bmob云后台获取TimerEvent数据失败");
                listener.onFail(4);
            }
        });
        eventAPI.getDataFromBmob(context);
    }





}
